package com.huawei.fastengine.fastview.download.download;

import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.DialogInterface.OnKeyListener;
import android.view.KeyEvent;
import com.huawei.fastengine.R;
import com.huawei.fastengine.fastview.download.utils.log.FastViewLogUtils;

public class GuideDownloadDlg {
    private static final String TAG = "GuideDownloadDlg";
    private AlertDialog dlg = null;

    interface Callback {
        void onNegative();

        void onPositive();
    }

    public void dismissGuideDownloadDlg() {
        if (this.dlg != null && this.dlg.isShowing()) {
            this.dlg.dismiss();
            this.dlg = null;
        }
    }

    public void showGuideDownloadDlg(Context context, final Callback callback) {
        if (this.dlg != null && this.dlg.isShowing()) {
            this.dlg.dismiss();
            this.dlg = null;
        }
        Builder builder = new Builder(context);
        builder.setMessage(R.string.fastapp_guide_download_apk);
        builder.setPositiveButton(R.string.fastapp_guide_download_next_step, new OnClickListener() {
            public void onClick(DialogInterface dialogInterface, int i) {
                if (callback != null) {
                    callback.onPositive();
                }
            }
        });
        builder.setNegativeButton(R.string.fastapp_exit_cancel, new OnClickListener() {
            public void onClick(DialogInterface dialogInterface, int i) {
                if (GuideDownloadDlg.this.dlg != null) {
                    GuideDownloadDlg.this.dlg.dismiss();
                    GuideDownloadDlg.this.dlg = null;
                }
                if (callback != null) {
                    callback.onNegative();
                }
            }
        });
        this.dlg = builder.create();
        this.dlg.setOnKeyListener(new OnKeyListener() {
            public boolean onKey(DialogInterface dialogInterface, int i, KeyEvent keyEvent) {
                if (GuideDownloadDlg.this.dlg == null || !GuideDownloadDlg.this.dlg.isShowing()) {
                    FastViewLogUtils.i(GuideDownloadDlg.TAG, "click key back, other case.");
                    return false;
                }
                FastViewLogUtils.i(GuideDownloadDlg.TAG, "click key back, cancel guide down dialog dismiss.");
                GuideDownloadDlg.this.dlg.dismiss();
                GuideDownloadDlg.this.dlg = null;
                if (callback != null) {
                    callback.onNegative();
                }
                return true;
            }
        });
        this.dlg.show();
    }
}
